package com.zhxi.gulimall.product.controller;

import com.zhxi.common.utils.PageUtils;
import com.zhxi.common.utils.R;
import com.zhxi.gulimall.product.entity.AttrEntity;
import com.zhxi.gulimall.product.entity.AttrGroupEntity;
import com.zhxi.gulimall.product.service.AttrAttrgroupRelationService;
import com.zhxi.gulimall.product.service.AttrGroupService;
import com.zhxi.gulimall.product.service.AttrService;
import com.zhxi.gulimall.product.vo.AttrGroupRelationVo;
import com.zhxi.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @Author: zhxi
 * @Email: 664480310@qq.com
 * @Date: 2024-08-19 17:41:15
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService relationService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 分类id查询列表
     *
     * @param params    分页参数
     * @param catelogId 属性id
     * @return 列表
     */
    @GetMapping("/list/{catelogId}")
    public R listByCateId(@RequestParam Map<String, Object> params, @PathVariable long catelogId) {
        PageUtils page = attrGroupService.listByCateId(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        return attrGroupService.info(attrGroupId);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

    /**
     * 分类id查询列表
     *
     * @param attrgroupId 属性id
     * @return 列表
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable long attrgroupId) {
        List<AttrEntity> attrEntityList = attrService.attrRelation(attrgroupId);
        return R.ok().put("data", attrEntityList);
    }
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params,@PathVariable long attrgroupId) {
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page", page);
    }


    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation((Arrays.asList(vos)));
        return R.ok();
    }
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }
    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){

        //1、查出当前分类下的所有属性分组，
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }


}
